//index.js
//获取应用实例
const app = getApp()
let col1H=0;
let col2H=0;
Page({
  data: {
    scrollH: 0,
    col1: [],
    col2: [],
    images: [],
    imgWidth: 0,
    loadingCount:0
  },
  //事件处理函数

  onLoad: function () {
    wx.getSystemInfo({
      success: (res) => {
        let ww = res.windowWidth;
        let wh = res.windowHeight;
        let imgWidth = ww * 0.48;
        let scrollH = wh;
        console.log(ww)
        this.setData({
          scrollH: scrollH,
          imgWidth: imgWidth

        });
        this.loadImages();
      },
    })
  },
  loadImages: function () {
    let images = [
      { pic: "../../images/1.jpg",height:0 },
      { pic: '../../images/2.jpg', height:0 },
      { pic: '../../images/3.jpg', height:0 },
      { pic: '../../images/4.jpg', height:0 },
      { pic: '../../images/5.jpg', height:0 },
      { pic: '../../images/6.jpg', height:0 },
      { pic: '../../images/7.jpg', height:0 },
      { pic: '../../images/8.jpg', height:0 },
      { pic: '../../images/9.jpg', height:0 },
      { pic: '../../images/10.jpg', height:0 },
      { pic: '../../images/11.jpg', height:0 },
      { pic: '../../images/12.jpg', height:0 },
      { pic: '../../images/13.jpg', height:0 },
      { pic: '../../images/14.jpg', height:0 },
      { pic: '../../images/15.jpg', height:0 },
      { pic: '../../images/16.jpg', height: 0 },
      { pic: '../../images/17.jpg', height: 0 },
      { pic: '../../images/18.jpg', height: 0 },
      { pic: '../../images/19.jpg', height: 0 },
      { pic: '../../images/20.jpg', height: 0 },
      { pic: '../../images/22.jpg', height: 0 },
      { pic: '../../images/21.jpg', height: 0 },
      { pic: '../../images/24.jpg', height: 0 },
      { pic: '../../images/26.jpg', height: 0 },
      { pic: '../../images/25.jpg', height: 0 }

     
    ];
    let baseId = 'img-' + (+new Date());
    for (let i = 0; i < images.length; i++) {
      images[i].id = baseId + '-' + i;
    }
    this.setData({
      images: images,loadingCount:images.length
    })
  },
  onImageLoad: function (e) {
    console.log(e)
    let imageId = e.currentTarget.id;
    let oImageH = e.detail.height;
    let oImageW = e.detail.width;
    let imgWidth = this.data.imgWidth;
  
    let scale = imgWidth/oImageW;
    console.log(scale)
    let imageHeight = scale * oImageH;
    console.log(imageHeight)
    let images = this.data.images;
    let imageObj = null;
    for (let i = 0; i < images.length; i++) {
      let img = images[i];
      if (img.id === imageId) {
        imageObj = img;
        break;
      }
    }
    imageObj.height = imageHeight;
    console.log(imageHeight)
    let loadingCount=this.data.loadingCount-1;
    let col1=this.data.col1;
    let col2 =this.data.col2;
  if(col1H<=col2H){
    col1H +=imageHeight;
    col1.push(imageObj);

  }else{
    col2H += imageHeight;
    col2.push(imageObj);
  }
  let data={loadingCount:loadingCount,
  col1:col1,
  col2:col2};
  if(!loadingCount){
    data.images=[]

  }
  this.setData(data)
  },
  onShareAppMessage(){
    return{
      title:'美女图片'
    }
  }

})
